package org.example.myleet.rosalind.utils;

import java.util.HashMap;
import java.util.Map;

public class AminoAcidScoringReader {

    /**
     * https://rosalind.info/glossary/blosum62/
     */
    private static final String BLOSUM62 = "" +
            "   A  C  D  E  F  G  H  I  K  L  M  N  P  Q  R  S  T  V  W  Y\n" +
            "A  4  0 -2 -1 -2  0 -2 -1 -1 -1 -1 -2 -1 -1 -1  1  0  0 -3 -2\n" +
            "C  0  9 -3 -4 -2 -3 -3 -1 -3 -1 -1 -3 -3 -3 -3 -1 -1 -1 -2 -2\n" +
            "D -2 -3  6  2 -3 -1 -1 -3 -1 -4 -3  1 -1  0 -2  0 -1 -3 -4 -3\n" +
            "E -1 -4  2  5 -3 -2  0 -3  1 -3 -2  0 -1  2  0  0 -1 -2 -3 -2\n" +
            "F -2 -2 -3 -3  6 -3 -1  0 -3  0  0 -3 -4 -3 -3 -2 -2 -1  1  3\n" +
            "G  0 -3 -1 -2 -3  6 -2 -4 -2 -4 -3  0 -2 -2 -2  0 -2 -3 -2 -3\n" +
            "H -2 -3 -1  0 -1 -2  8 -3 -1 -3 -2  1 -2  0  0 -1 -2 -3 -2  2\n" +
            "I -1 -1 -3 -3  0 -4 -3  4 -3  2  1 -3 -3 -3 -3 -2 -1  3 -3 -1\n" +
            "K -1 -3 -1  1 -3 -2 -1 -3  5 -2 -1  0 -1  1  2  0 -1 -2 -3 -2\n" +
            "L -1 -1 -4 -3  0 -4 -3  2 -2  4  2 -3 -3 -2 -2 -2 -1  1 -2 -1\n" +
            "M -1 -1 -3 -2  0 -3 -2  1 -1  2  5 -2 -2  0 -1 -1 -1  1 -1 -1\n" +
            "N -2 -3  1  0 -3  0  1 -3  0 -3 -2  6 -2  0  0  1  0 -3 -4 -2\n" +
            "P -1 -3 -1 -1 -4 -2 -2 -3 -1 -3 -2 -2  7 -1 -2 -1 -1 -2 -4 -3\n" +
            "Q -1 -3  0  2 -3 -2  0 -3  1 -2  0  0 -1  5  1  0 -1 -2 -2 -1\n" +
            "R -1 -3 -2  0 -3 -2  0 -3  2 -2 -1  0 -2  1  5 -1 -1 -3 -3 -2\n" +
            "S  1 -1  0  0 -2  0 -1 -2  0 -2 -1  1 -1  0 -1  4  1 -2 -3 -2\n" +
            "T  0 -1 -1 -1 -2 -2 -2 -1 -1 -1 -1  0 -1 -1 -1  1  5  0 -2 -2\n" +
            "V  0 -1 -3 -2 -1 -3 -3  3 -2  1  1 -3 -2 -2 -3 -2  0  4 -3 -1\n" +
            "W -3 -2 -4 -3  1 -2 -2 -3 -3 -2 -1 -4 -4 -2 -3 -3 -2 -3 11  2\n" +
            "Y -2 -2 -3 -2  3 -3  2 -1 -2 -1 -1 -2 -3 -1 -2 -2 -2 -1  2  7";

    public static Map<String, Integer> getScoringMap() {
        Map<String, Integer> scoringMap = new HashMap<>(20 * 20);
        String[] lines = BLOSUM62.split("\n");
        //parse table head
        char[] aa = new char[20];
        int aai = 0;
        String tableHead = lines[0];
        for (int i = 0; i < tableHead.length(); ++i) {
            char c = tableHead.charAt(i);
            if (c != ' ') {
                aa[aai++] = c;
            }
        }
        StringBuilder sb = new StringBuilder();
        char rowHead = 'Z';
        for (int row = 1; row < lines.length; ++row) {
            String line = lines[row];
            aai = 0;
            for (int i = 0; i < line.length(); ++i) {
                char c = line.charAt(i);
                if (Character.isAlphabetic(c)) {
                    rowHead = c;
                } else if (c == '-' || Character.isDigit(c)) {
                    sb.append(c);
                } else if (c == ' ' && sb.length() > 0) {
                    int score = Integer.parseInt(sb.toString());
                    scoringMap.put(aa[aai++] + "" + rowHead, score);
                    sb.delete(0, sb.length());
                }
            }
            if (sb.length() > 0) {
                int score = Integer.parseInt(sb.toString());
                scoringMap.put(aa[aai] + "" + rowHead, score);
                sb.delete(0, sb.length());
            }
        }
        return scoringMap;
    }

    public static void main(String[] args) {
        System.out.println(getScoringMap());
    }
}
